package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException {
        //1.类名.class
//        Class cls = String.class;
//        Class cls = ArrayList.class;

        //2:Class,forName()
//        Class cls = Class.forName("java.lang.String");
//        Class cls = Class.forName("java.util.ArrayList");

        /*
            java.util.ArrayList
            java.util.HashMap
            java.io.FileInputStream
            java.lang.String
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);


        //获取类的完全限定名(包名.类名)
        String name = cls.getName();
        System.out.println(name);
        //进获取类名
        name = cls.getSimpleName();
        System.out.println(name);

        //通过类对象可以获取其表示类的包信息
        Package pack = cls.getPackage();
        System.out.println(pack.getName());

        //类对象的getMethods()方法可以获取其表示的类中所有公开方法(包含从超类继承的)
        Method[] methods = cls.getMethods();
        System.out.println("一共"+methods.length+"个方法");
        for (Method method : methods){
            System.out.println(method.getName());
        }
    }
}
